<script setup lang="ts">
import KnowledgeListItem from './KnowledgeListItem.vue'
import type { ConsultType, ConsultParams, ConsultItem } from '@/types/consult'
import { getConsultList } from '@/services/consult'
import { ref } from 'vue'
const props = defineProps<{
  type: ConsultType
}>()
// 请求参数
const params = ref<ConsultParams>({
  current: 1,
  pageSize: 10,
  type: props.type
})

// 上拉加载更多
// 1、van-list组件将列表项进行包裹
// 2、定义loading 定义finished
// 3、定义load方法加载数据 并绑定
// 4、请求数据 并且将请求回来的数据 追加到数组的后面
// 5、手动将loading变为false
// 6、处理加载完成的情况 pageTotal ||  Total list.length
const loading = ref(false)
const finished = ref(false)
// 返回值
const list = ref<ConsultItem[]>([])
// 获取数据
const getList = async () => {
  const res = await getConsultList(params.value)
  list.value = [...list.value, ...res.data.rows]
  loading.value = false
  params.value.current++
  if (params.value.current >= res.data.pageTotal) {
    finished.value = true
  }
}
</script>

<template>
  <div class="knowledge-list">
    <van-list
      v-model:loading="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="getList"
    >
      <KnowledgeListItem v-for="item in list" :key="item.id" :item="item" />
    </van-list>
  </div>
</template>

<style lang="scss" scoped>
.knowledge-list {
  padding: 0 15px;
}
</style>
